 
function planeGame() {
	
	if (!canvasSupport()) {
			 return;
  		}
	var displayList		= new DisplayList();
	
	
	var BLOCK_START_X = 40;
	var BLOCK_START_Y = 50;
	var BLOCK_Y_SPACING = 10;
	var BLOCK_WIDTH = 25;
	var BLOCK_HEIGHT = 25;
	
	
	var titleScreen		= null;
	var backGround;
	setInterval(run, 33);	
	
	var blockColors = new Array ("red","blue","green","yellow","orange","pink","purple");
	var blockHTMLColors = new Array("#FF0000","#0000FF","#00FF00","#FFFF00","#FF6600","#FF33CC","#990099");
	
	
	var STATE_SHOW_TITLE_SCREEN		= 30;
	var STATE_WAIT_FOR_CLOSE		= 40;
	var STATE_RESET_GAME			= 50;
	var STATE_PLAY_GAME		= 80;
	var STATE_WAIT					= 150;

	
	var waitFrames = 0;
	var waitCount  = 0;
	var waitNextState = 0;
	
	var shotWaitFrames = 3;
	var shotWaitCount  = 3;

	var _this = this;
	var player;
	var shots = new Array();
	var shotPool = new Array();
	var enemies = new Array();
	var enemyPool = new Array();
	
	
	var oPaused= false;
	var oSound = "on";
	
  	var theCanvas = document.getElementById("canvasOne");
	var context = theCanvas.getContext("2d");
	var gameState = STATE_SHOW_TITLE_SCREEN;
	theCanvas.addEventListener("mouseup",onMouseUp, false);	
	theCanvas.addEventListener("mousemove",onMouseMove, false);	
	
	var soundManager	= new SoundManager(10);
	soundManager.addSound("explode1",3);
	soundManager.addSound("shoot2",3);
	soundManager.addSound("soundtrack",1);
	var ocean		= new Ocean(context,0,theCanvas.width,-100,theCanvas.height+100,3,3);
	var cloud		= new Cloud(context,0,theCanvas.width,-100,theCanvas.height+100,7,5);
	
	
	var particleManager = new ParticleExplosionManager(context);
	particleManager.setImage("particle.gif");
  
  
  function showTitleScreen() {
	titleScreen = new DisplayScreen(0,0,theCanvas.width, theCanvas.height, "#083184", "#000000", context, 1,"title.gif","button_start.gif", theCanvas);
	titleScreen.addEventListener(titleScreen.EVENT_CLOSE, onTitleScreenClosed);
	displayList.addChild(titleScreen);
	gameState = STATE_WAIT_FOR_CLOSE;
  }
  
  function onTitleScreenClosed() {
	  titleScreen.removeEventListener(titleScreen.EVENT_CLOSE, onTitleScreenClosed);
	  displayList.removeChild(titleScreen);
	  gameState = STATE_RESET_GAME;
  }
  
  function resetGame() {
	 soundManager.playSound("soundtrack",.8,true);
	 player = new Sprite("player.gif",59,42,context,theCanvas.width/2, theCanvas.height-100,0);
	 displayList.addChild(player);	
	  
	 
	 backGround = new Image();
	 backGround.src = "background.gif";
	 gameState = STATE_PLAY_GAME;
 	
   }
   

   
  
 	 function playGame() {
		 shotWaitCount ++;
		 createEnemies();
		 moveObjects();
		 checkCollisions();
  
  	}
	
	function createEnemies() {
		var rnd = Math.floor(Math.random() * 100);
		if (rnd >= 95) {
			var rndx =Math.floor(Math.random() * (theCanvas.width-60)-0) + 0;
			rndEnemy = Math.floor(Math.random() * 100);
			if (rndEnemy > 40) {
				var tempEnemy = new Sprite("enemy.gif",57,45,context,rndx,-45,6);
				tempEnemy.size=1;
			} else if (rndEnemy > 10) {
				var tempEnemy = new Sprite("enemy2.gif",61,48,context,rndx,-45,5);
				tempEnemy.size=2;
			} else {
				var tempEnemy = new Sprite("enemy3.gif",95,77,context,rndx,-45,4);
				tempEnemy.size=3;
			
			}
				
			enemies.push(tempEnemy);
			displayList.addChild(tempEnemy);
		}
	}
  
  	function moveObjects() {
		for (var i=shots.length-1;i >= 0;i--) {
			shots[i].y -= shots[i].speed;
			if (shots[i].y < -shots[i].height) {
				displayList.removeChild(shots[i]);	
				shots.splice(i,1);
			}
		}
		
		for (var i=enemies.length-1;i >= 0;i--) {
			enemies[i].y += enemies[i].speed;
			if (enemies[i].y < -enemies[i].height) {
				displayList.removeChild(enemies[i]);	
				enemies.splice(i,1);
			}
		}
 
	}
 
 	function checkCollisions() {
		shot:for (var i=shots.length-1;i >= 0;i--) {
			for (var j=enemies.length-1;j >= 0;j--) {
				if (hitTest(shots[i],enemies[j])) {
					displayList.removeChild(shots[i]);
					displayList.removeChild(enemies[j]);
					soundManager.playSound("explode1",1,false);
					var rndParts = Math.floor(Math.random()*10) + 10;		
					particleManager.createExplosion(shots[i].x,shots[i].y,"#FF0000", rndParts*enemies[j].size,3,3,6,.2,20);
					//particleManager.createExplosion(shots[i].x,shots[i].y,"#FFFF00", rndParts,3,3,4,.2,20);
					shots.splice(i,1);
					enemies.splice(j,1);
					break shot;
				}
			}
		}
		
	
	}
	
	function onMouseUp(event) {
		//stopDragging();
		displayList.onMouseUp(event);
		if (shotWaitCount >= shotWaitFrames && gameState == STATE_PLAY_GAME) {
			shotWaitCount = 0;
			var tempShot = new Sprite("shot.gif",9,19,context,player.x+player.width/2,player.y,10);
			shots.push(tempShot);
			displayList.addChild(tempShot);
			soundManager.playSound("shoot1",.4,false);	
		}
	}
	
	function onMouseMove(event) {
		var mouseX;
		var mouseY;
			
		if ( event.layerX ||  event.layerX == 0) { // Firefox
			mouseX = event.layerX ;
			mouseY = event.layerY;
		} else if (event.offsetX || event.offsetX == 0) { // Opera
			mouseX = event.offsetX;
			mouseY = event.offsetY;
		}
		
		if (gameState == STATE_PLAY_GAME) {
			player.x = mouseX;
			player.y = mouseY;
		}
	
	}
	

  	
  function  draw () {
 		
		
		//Background
		context.fillStyle = "#000000";
		context.fillRect(0, 0, theCanvas.width, theCanvas.height);
		//Box
		context.strokeStyle = "#FF2020"; 
		context.strokeRect(1,  1, theCanvas.width-2, theCanvas.height-2);
		if (gameState == STATE_PLAY_GAME) {
			context.drawImage(backGround,0,0);
		}
		if (gameState == STATE_PLAY_GAME) {
			ocean.draw();
		}
		particleManager.draw();
		displayList.draw();
		if (gameState == STATE_PLAY_GAME) {
			cloud.draw();
		}
		
		
		
		//Draw Text
		context.fillStyle    = "#FFFFFF";
		context.font = " 10px serif";
		context.fillText  ("(p)aused: " + oPaused,  10 ,465);
		context.fillText  ("(s)ound: " + oSound,  70 ,465);
  			
	}
	
	function run() {
		if (!oPaused) {
			switch(gameState) {
				case STATE_SHOW_TITLE_SCREEN:
					showTitleScreen();
					break;
				case STATE_WAIT_FOR_CLOSE:
					break;					
				case STATE_RESET_GAME:
					resetGame();
					break;	
				case STATE_PLAY_GAME:
					playGame();
					break;			
				case STATE_WAIT:
					wait();
					break;			 	
			}
  			draw();
		}
		
  }
  
  
  function wait() {
	  waitCount++
	  if (waitCount >= waitFrames) {
		gameState = waitNextState;
	  }
  
  }
  

	
	document.onkeydown = function(e) {
		if (e.keyCode == 80) {
			
			if (oPaused == true) {
				oPaused = false;
			} else {
				oPaused = true;
			}
				
			//pause
		} else if(e.keyCode == 83) {
			if (oSound == "on") {
				oSound = "off";
				soundManager.setMuted(true);
			} else {
				oSound = "on";
				soundManager.setMuted(false);
			}
		}
		draw();
	}
	
	
}


